<?php

class Core_Model_Table_Session extends Model_Table implements Zend_Session_SaveHandler_Interface
{
    /**
     * life time in seconds
     * @var int
     *
     */
    protected $_lifetime = null;

    /**
     * session save path
     * @var string
     */
    protected $_sessionSavePath = '';

    /**
     * session name
     * @var string
     */
    protected $_sessionName = '';

    /**
     * @var int
     */
    protected $_remember = null;

    /**
     * session name
     * @var string
     */
    protected $_name = 'core_session';

    /**
     * Destructor
     *
     * @return void
     */
    public function __destruct()
    {
        session_write_close();
    }

    public function remember($seconds)
    {
        $this -> _remember = $seconds;
        return $this;
    }

    public function setLifetime($lifetime)
    {
        $lifetime = (int)$lifetime;

        if ($lifetime <= 0)
        {
            $lifetime = ini_get('session.gc_maxlifetime');

        }

        $this -> _lifetime = $lifetime;
        return $this;
    }

    /**
     * @return lifetime
     */
    public function getLifetime()
    {
        if (null == $this -> _lifetime)
        {
            $this -> _lifetime = ini_get('session.gc_maxlifetime');
        }
        return $this -> _lifetime;
    }

    /**
     * Open Session
     *
     * @param string $save_path
     * @param string $name
     * @return boolean
     */
    public function open($save_path, $name)
    {
        $this -> _sessionSavePath = $save_path;
        $this -> _sessionName = $name;

        return true;
    }

    /**
     * Close session
     *
     * @return boolean
     */
    public function close()
    {
        return true;
    }

    /**
     * Read session data
     *
     * @param string $id
     * @return string
     */
    public function read($id)
    {
        $return = '';

        $row = $this -> find($id) -> current();

        if (is_object($row))
        {
            $return = $row -> data;
        }

        return $return;
    }

    /**
     * Write session data
     *
     * @param string $id
     * @param string $data
     * @return boolean
     */
    public function write($id, $data)
    {
        $result = 1;
        try
        {
            $row = $this -> find($id) -> current();

            if (!is_object($row))
            {
                $row = $this -> fetchNew();
                $row -> id = $id;
                $row -> lifetime = $this -> getLifetime();
            }

            if (null != $this -> _remember)
            {
                $row -> lifetime = $this -> _remember;
            }

            $row -> modified = time();

            $row -> data = (string)$data;

            $row -> save();
        }
        catch(Exception $e)
        {
            if (DEBUG)
            {
                echo $e -> getMessage();
            }
            $result = 0;
        }

        return $result;
    }

    /**
     * Destroy session
     *
     * @param string $id
     * @return boolean
     */
    public function destroy($id)
    {
        $this -> delete('id=' . (string)$id);
        return 1;
    }

    /**
     * Garbage Collection
     *
     * @param int $maxlifetime
     * @return true
     */
    public function gc($maxlifetime)
    {
        $this -> delete('modified + lifetime < ' . time());
        return 1;
    }

    public function removeSessionByUserId($id)
    {
        $this -> delete(array('user_id = ?' => $authId, ));
        return $this;
    }

}
